<?php

/**
 * @BelongProject: flami_mangers
 * @Author: Zwc
 * @CreateTime: 2025-10-30 19:33:36
 * @Description: TODO
 */

namespace app\admin\controller;

use app\admin\logic\LangTextLogic;
use app\admin\model\LangTextModel;
use app\admin\service\CountryService;
use cmf\controller\AdminBaseController;

class LangTextController extends AdminBaseController
{
    /**
     * 获取数据
     * @return void
     * @throws \think\db\exception\DbException
     */
    public function index()
    {
        $params = request()->get();
        $countryData = $this->recommendLanguageData;
        if (empty($params['langId'])) {
            $params['langId'] = $countryData[0]['id'];
        }
        $langTextLogic = new LangTextLogic();
        $list = $langTextLogic->getData($params);

        $list->appends($params);

        $page = $list->render();
        $this->assign('page', $page);
        $this->assign('list', $list);
        $this->assign('countryData', $this->recommendLanguageData);
        if (!empty($params['langId'])) {
            $this->assign('langId', $params['langId']);
        }else {
            $this->assign('langId', $countryData[0]['id']);
        }
        return $this->fetch();
    }

    /**
     * 添加
     * @return void
     */
    public function add()
    {
        if (request()->isPost()) {
            $params = $this->request->post();
            if (empty($params['lang_id'])) {
                $this->error('请选择语言');
            }

            if (empty($params['type'])) {
                $this->error('请选择文本类型');
            }

            if (empty($params['value'])) {
                $this->error('文本内容不能为空');
            }
            $langTextModel = new LangTextModel();
            $result = $langTextModel->insert($params);
            $this->success("操作成功");
        }else {
            $this->assign('typeList', (new LangTextLogic())->getTypeArr());
            $this->assign('langData', $this->recommendLanguageData);
            return $this->fetch();
        }
    }

    /**
     * 修改
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit()
    {
        if (request()->isPost()) {
            $params = $this->request->post();

            if (empty($params['type'])) {
                $this->error('请选择文本类型');
            }

            if (empty($params['value'])) {
                $this->error('文本内容不能为空');
            }
            $langTextModel = new LangTextModel();
            $result = $langTextModel->where(['id' => $params['id']])->data($params)->save();
            $this->success("操作成功");
        }else {
            $params = request()->get();
            $langTextModel = new LangTextModel();
            $data = $langTextModel->where(['id' => $params['id']])->find();
            $this->assign('data', $data);
            $this->assign('typeList', (new LangTextLogic())->getTypeArr());
            $this->assign('langData', $this->recommendLanguageData);
            return $this->fetch();
        }
    }

    /**
     * 删除
     * @return void
     */
    public function del()
    {
        $params = request()->get();
        $langTextModel = new LangTextModel();
        $data = $langTextModel->where(['id' => $params['id']])->data(['del_flag' => 1])->save();
        $this->success("操作成功");
    }


}